﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.APP.Reports;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class RPT_Exports : System.Web.UI.Page
    {
        public void Page_Load(object sender, EventArgs e)
        {
            
        }
    
        public static DataTable dtX = null;
        public static object dtR = null;
        protected void LoadReport(string reportName)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtReport"];
            if (null == dtX) dtX = dt;

            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("~/Reports/" + reportName + ".rpt"));     
            
            //โหลดข้อมูล เข้า SubReport
            if (Session["dtSubReport"] != null)
            {
                string[] strSubReportName = (string[])Session["strSubReportName"];
                DataTable[] dtSubReport = (DataTable[])Session["dtSubReport"];

                for (int i = 0; i < rpt.Subreports.Count; i++)
                {
                    rpt.OpenSubreport(strSubReportName[i] + ".rpt");
                    rpt.Subreports[strSubReportName[i] + ".rpt"].SetDataSource(dtSubReport[i].DefaultView);
                }               
                //Session["dtSubReport"] = null;
            }

            try
            {
                //โหลดข้อมูล เข้า MainReport
                rpt.SetDataSource(dt ==null ? dtX.DefaultView : dt.DefaultView);
                //Session["dtReport"] = null;
            }
            catch (Exception error)
            {
            }

            SetParameter(ref rpt, reportName);

            //เก็บ ReportSource บน Session
            ViewState["ReportSource"] = rpt;

            CrystalReportViewer1.ReportSource = dtR =
                (CrystalDecisions.CrystalReports.Engine.ReportDocument)ViewState["ReportSource"];

        }

        private void SetParameter(ref ReportDocument rpt, string reportName)
        {
            if (reportName == "RPT_PS_EmployeeDtls")
            {
                rpt.SetParameterValue("AdmitDate", GetLocalResourceObject("captionAdmitDate"));
                rpt.SetParameterValue("RetryDate", GetLocalResourceObject("captionRetryDate"));
                rpt.SetParameterValue("AdmitPosDate", GetLocalResourceObject("captionAdmitPosDate"));
                rpt.SetParameterValue("Jobsite1", GetLocalResourceObject("captionJobsite1"));
                rpt.SetParameterValue("Jobsite2", GetLocalResourceObject("captionJobsite2"));
            }
        }

        protected void CrystalReportViewer1_Init(object sender, EventArgs e)
        {

            string reportName = Request.QueryString["ReportName"];

            if (ViewState["ReportSource"] == null)
            {
                LoadReport(reportName);
            }
            else
            {
                CrystalReportViewer1.ReportSource = (CrystalDecisions.CrystalReports.Engine.ReportDocument)this.ViewState["ReportSource"];
            }

        }

    }
}
